<?php
/**
 * Created by PhpStorm.
 * User: longli
 * VX: isa1589518286
 * Date: 2021/08/23
 * Time: 23:53
 * @link http://www.lmterp.cn
 */

namespace app\common\model;

/**
 * 物流配置规则
 * Class ChannelRules
 * @package app\common\model
 */
class OrdersRules extends BaseModel
{
    protected $pk = 'rule_id';

    protected $autoWriteTimestamp = 'datetime';

    protected $insert = ['create_by', 'update_by'];

    protected $update = ['update_by'];

    protected $append = ['channel'];

    /**
     * 平台
     * @var int
     */
    const RULE_TYPE_PLATFORM = 0;

    /**
     * 账号
     * @var int
     */
    const RULE_TYPE_ACCOUNT = 10;

    /**
     * SPU
     * @var int
     */
    const RULE_TYPE_SPU = 20;

    /**
     * SKU
     * @var int
     */
    const RULE_TYPE_SKU = 30;

    /**
     * SKU前缀
     * @var int
     */
    const RULE_TYPE_SKU_PREFIX = 40;

    /**
     * SKU后缀
     * @var int
     */
    const RULE_TYPE_SKU_SUFFIX = 50;

    /**
     * 重量段
     * @var int
     */
    const RULE_TYPE_WEIGHT = 60;

    /**
     * 价格段
     * @var int
     */
    const RULE_TYPE_MONEY = 70;

    /**
     * 国家
     * @var int
     */
    const RULE_TYPE_COUNTRY = 80;

    /**
     * 最迟发货
     * @var int
     */
    const RULE_TYPE_LAST_SEND = 90;

    /**
     * 物流属性
     * @var int
     */
    const RULE_TYPE_ATTR = 100;


    public static $RULE_TYPE = [
        self::RULE_TYPE_PLATFORM    => '平台',
        self::RULE_TYPE_ACCOUNT     => '账号',
        self::RULE_TYPE_COUNTRY     => '国家',
        self::RULE_TYPE_SPU         => 'SPU',
        self::RULE_TYPE_SKU         => 'SKU',
        self::RULE_TYPE_SKU_PREFIX  => 'SKU前缀',
        self::RULE_TYPE_SKU_SUFFIX  => 'SKU后缀',
        self::RULE_TYPE_WEIGHT      => '重量段',
        self::RULE_TYPE_MONEY       => '价格段',
        self::RULE_TYPE_LAST_SEND   => '最尺发货',
        self::RULE_TYPE_ATTR        => '物流属性',
    ];

    protected function getRuleTypeAttr($value)
    {
        return isset(self::$RULE_TYPE[$value]) ? self::$RULE_TYPE[$value] : $value;
    }

    /**
     * 并且
     * @var int
     */
    const MATCH_TYPE_AND = 1;

    /**
     * 或者
     * @var int
     */
    const MATCH_TYPE_OR = 2;

    /**
     * 结束
     * @var int
     */
    const MATCH_TYPE_END = 3;

    public static $MATCH_TYPE = [
        self::MATCH_TYPE_AND => '并且',
        self::MATCH_TYPE_OR  => '或者',
        self::MATCH_TYPE_END => '结束',
    ];

    protected function getMatchTypeAttr($value)
    {
        return isset(self::$MATCH_TYPE[$value]) ? self::$MATCH_TYPE[$value] : $value;
    }

    /**
     * 在列表
     * @var int
     */
    const CONDITION_IN = 0;

    /**
     * 不在列表
     * @var int
     */
    const CONDITION_NOT_IN = 10;

    /**
     * 在范围内
     * @var int
     */
    const CONDITION_BETWEEN = 20;

    /**
     * 等于
     * @var int
     */
    const CONDITION_EQ = 30;

    /**
     * 不等于
     * @var int
     */
    const CONDITION_NEQ = 40;

    /**
     * 大于
     * @var int
     */
    const CONDITION_GT = 50;

    /**
     * 大于等于
     * @var int
     */
    const CONDITION_EGT = 60;

    /**
     * 小于
     * @var int
     */
    const CONDITION_LT = 70;

    /**
     * 小于等于
     * @var int
     */
    const CONDITION_ELT = 80;


    public static $CONDITION_TYPE = [
        self::CONDITION_IN       => '在列表',
        self::CONDITION_NOT_IN   => '不在列表',
        self::CONDITION_BETWEEN  => '在范围内',
        self::CONDITION_EQ       => '等于',
        self::CONDITION_NEQ      => '不等于',
        self::CONDITION_GT       => '大于',
        self::CONDITION_EGT      => '大于等于',
        self::CONDITION_LT       => '小于',
        self::CONDITION_ELT      => '小于等于',
    ];

    /**
     * 条件
     * @param $value
     * @return string
     * @date 2021/09/30
     * @author longli
     */
    protected function getConditionAttr($value)
    {
        return isset(self::$CONDITION_TYPE[$value]) ? self::$CONDITION_TYPE[$value] : $value;
    }

    /**
     * 渠道
     * @return Channel[]
     * @date 2021/09/30
     * @author longli
     */
    protected function getChannelAttr()
    {
        return Channel::whereIn("channel_id", $this->list_id)
            ->select();
    }

    /**
     * 仓库
     * @return Warehouse
     * @date 2021/09/30
     * @author longli
     */
    protected function getWarehouseAttr()
    {
        return Warehouse::get($this->list_id);
    }



    /**
     * 关联规则组
     * @return \think\model\relation\BelongsTo
     * @date 2021/08/24
     * @author longli
     */
    public function rgroup()
    {
        return $this->belongsTo(OrdersRulesGroup::class, 'group_id', 'group_id');
    }
}
